package com.bytecodezz.javacase.trycatch;

/**
 * try-catch 耗时统计
 * 测试数据：100亿次循环
 * 内部try-catch 耗时 5.8秒
 * 外部try-catch 耗时 4.8秒 相差1秒左右
 * 结论：相差不大，字节码指令中看出，try-catch每个方法都有对应的异常表，出现异常直接查表获得跳转指令行即可
 */
public class TryCatchTime {
    public static final long COUNT = 100_0000_0000L;

    public static void main(String[] args) {
        long startTime = System.nanoTime();
        int a = 0;
        for (long i = 0; i < COUNT; i++) {
            try {
                a++;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println("try-case-inner:" + (System.nanoTime() - startTime) / 100_0000 + " ms");
        startTime = System.nanoTime();
        int b = 0;
        try {
            for (long i = 0; i < COUNT; i++) {
                b++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("try-case-out:" + (System.nanoTime() - startTime) / 100_0000 + " ms");
    }
}
